package com.media.revision.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.media.AlbumManageUtils;
import com.media.R;
import com.media.image.ImageModel;
import com.media.image.ImageWork;
import com.media.image.SquareImageView;
import com.media.utils.MediaCornerImageView;

import java.util.ArrayList;

/**
 * 底部选中列表
 *
 * @author 关忆北丶
 * @description:
 * @date :2020/9/6 14:03
 */
public class SelectListAdapter extends RecyclerView.Adapter<SelectListAdapter.SelectItemHolder> {
    private LayoutInflater mLatout;
    private Activity mActivity;
    public ImageWork mImageWork;
    private ArrayList<ImageModel> selecImageList = new ArrayList<>();//选中图片


    public SelectListAdapter(LayoutInflater mLatout, Activity mActivity, ImageWork mImageWork) {
        this.mLatout = mLatout;
        this.mActivity = mActivity;
        this.mImageWork = mImageWork;
    }

    public void setSelecImageList(ArrayList<ImageModel> selecImageList) {
        this.selecImageList = selecImageList;
    }

    @NonNull
    @Override
    public SelectItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new SelectItemHolder(mLatout.inflate(R.layout.item_selectlist, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectItemHolder selectItemHolder, @SuppressLint("RecyclerView") final int i) {
        final ImageModel imageModel = selecImageList.get(i);
        mImageWork.loadImage(imageModel.getType(), imageModel.getPath(), selectItemHolder.mIvSelectitemPhoto);
        if (imageModel.getType().equals(ImageModel.TYPE_IMAGE)) {
            selectItemHolder.mTvVideoTime.setVisibility(View.GONE);
        } else if (imageModel.getType().equals(ImageModel.TYPE_VIDEO)) {
            selectItemHolder.mTvVideoTime.setText(AlbumManageUtils.stringForTime(imageModel.duration));
            selectItemHolder.mTvVideoTime.setVisibility(View.VISIBLE);
        }

        selectItemHolder.mIvSelectitemDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnDeleteSelectClickListener != null)
                    mOnDeleteSelectClickListener.onDeleteSelect(imageModel, i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return selecImageList.size();
    }


    public class SelectItemHolder extends RecyclerView.ViewHolder {
        private MediaCornerImageView mIvSelectitemPhoto;
        private ImageView mIvSelectitemDelete;
        private TextView mTvVideoTime;

        public SelectItemHolder(@NonNull View itemView) {
            super(itemView);
            mIvSelectitemPhoto = itemView.findViewById(R.id.iv_selectitem_photo);
            mIvSelectitemDelete = itemView.findViewById(R.id.iv_selectitem_delete);
            mTvVideoTime = itemView.findViewById(R.id.tv_video_time);
        }
    }

    private OnDeleteSelectClickListener mOnDeleteSelectClickListener;

    public interface OnDeleteSelectClickListener {
        void onDeleteSelect(ImageModel imageModel, int pos);
    }

    public void setOnDeleteSelectClickListener(OnDeleteSelectClickListener mOnDeleteSelectClickListener) {
        this.mOnDeleteSelectClickListener = mOnDeleteSelectClickListener;
    }
}
